using System;
using System.Collections.Generic;
using System.Text;



using Csla;
using Csla.Data;

using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
namespace buildingLib.fee
{
    public class FeeType :ReadOnlyBase<FeeType>
    {
        private Guid m_id;
        public Guid ID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_id;
            }
        }
        private Guid m_parentid;
        public Guid ParentID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_parentid;
            }
        }

        private string m_name;
        public string Name
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_name;
            }
        }

        bool m_isCategory;
        public bool IsCategory
        {
            get { return m_isCategory; }
        }

        bool m_isin;

        public bool IsIn
        {
            get { return m_isin; }
        }



        protected override object GetIdValue()
        {
            return m_id;
        }


        internal static FeeType GetFeeType(SafeDataReader dr)
        {
            return new FeeType(dr);
        }
        private FeeType(SafeDataReader dr)
        {
            m_name = dr.GetString("name");
            m_id = dr.GetGuid("id");
            m_parentid = dr.GetGuid("parentid");
            m_isCategory = dr.GetBoolean("iscategory");
            m_isin = dr.GetBoolean("isin");
        }

    }

    public class FeeTypes : ReadOnlyListBase<FeeTypes, FeeType>
    {
         #region Factory Methods

    public static FeeTypes GetFeeTypes()
    {
      return DataPortal.Fetch<FeeTypes>(new Criteria(string.Empty));
    }
        static FeeTypes types;
        public static FeeTypes GetObject()
        {
            if(types==null)
                types = DataPortal.Fetch<FeeTypes>(new Criteria(string.Empty));
            return types;
        }

        private FeeTypes()
    { /* require use of factory methods */ }

    #endregion

    #region Data Access

    [Serializable()]
    private class Criteria
    {
      private string _filter;
      public string Filter
      {
        get { return _filter; }
      }
      public Criteria(string filter)
      {
        _filter = filter;
      }
    }

    private void DataPortal_Fetch(Criteria criteria)
    {
      RaiseListChangedEvents = false;
      IsReadOnly = false;
      Database db = SQLDatabase.GetDatabase();
      DbCommand dbcom = db.GetSqlStringCommand(@"WITH vfeetype(parentid, id, name, LEVEL,iscategory,isin) AS (SELECT     parentid AS parentid, id, name, 0 AS LEVEL,iscategory,isin
                                                                                                 FROM         feetype
                                                                                                 WHERE     parentid IS  NULL
                                                                                                 UNION ALL
                                                                                                 SELECT     child.parentid, child.id, child.name, LEVEL + 1,child.iscategory,child.isin
                                                                                                 FROM         feetype child INNER JOIN
                                                                                                                       vfeetype parent ON child.parentid = parent.id
                                                                                                 )
    SELECT     *
     FROM         vfeetype order by LEVEL");

      using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(dbcom)))
      {
          while (dr.Read())
              Add(FeeType.GetFeeType(dr));
      }
      IsReadOnly = true;
      RaiseListChangedEvents = true;
    }

    #endregion
    }

    public class FeeTypeIDNameMap : NameValueListBase<Guid, string>
    {

        static FeeTypeIDNameMap m_List;
        public static FeeTypeIDNameMap GetLists()
        {
            return DataPortal.Fetch<FeeTypeIDNameMap>(new Criteria(typeof(FeeTypeIDNameMap)));
        }

        public static void InvalidateCache()
        {
            m_List = null;
        }


        private FeeTypeIDNameMap() { }
        #region DataAccess

        protected override void DataPortal_Fetch(object criteria)
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;
            string sql = "Select * from FeeType ";
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetSqlStringCommand(sql);

            using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(dbcom)))
            {
                while (dr.Read())
                {
                    Add(new NameValueListBase<Guid, string>.
                      NameValuePair(dr.GetGuid("id"), dr.GetString("name")));
                }
            }
            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }
        #endregion
    }
}
